package su.msk.jet.team01.chatik.server.commands;

import su.msk.jet.team01.chatik.server.data.Client;
import su.msk.jet.team01.chatik.server.exception.InvalidArgumentException;
import su.msk.jet.team01.chatik.server.exception.NameInUseException;

public class ChangeNameCommand extends Command {
	private static final int MAX_USER_NAME_LENGTH = 100;
	private static final boolean ALLOW_SPACES_IN_USER_NAMES = false;

	public ChangeNameCommand() {
		super("name", 1);
	}

	@Override
	public void execute(final Client client, final String[] args)
			throws InvalidArgumentException {
		String name = args[0];
		
		if (!ALLOW_SPACES_IN_USER_NAMES) {
			validateHaveNoSpaces(name);
		}
		try {
			validateLength(name, MAX_USER_NAME_LENGTH);
			client.changeName(name);
			client.send("Name successfully changed to %s.", name);
		} catch (InvalidArgumentException e) {
			client.send("Unable to change name. Maximum name length is %d.",
					MAX_USER_NAME_LENGTH);
		} catch (NameInUseException e) {
			client.send("Name \"%s\" is already in use.", name);
		}
	}

}
